package com.tarzan.gateway.controller;

import com.tarzan.common.core.base.BaseResponse;
import com.tarzan.common.core.exception.BusinessException;
import com.tarzan.gateway.service.GateWayDynamicRouteService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.gateway.route.RouteDefinition;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Package com.tarzan.gateway.controller
 * @Title GateWayDynamicRouteController
 * @Description TODO
 * @Author liujinshan
 * @CreateTime 2021/11/12 15:02
 */
@RestController
public class GateWayDynamicRouteController {

    private final static Logger logger = LoggerFactory.getLogger(GateWayDynamicRouteController.class);

    @Autowired
    private GateWayDynamicRouteService gateWayDynamicRouteService;


    /**
     * @title add
     * @description TODO 新增
     * @param routeDefinition
     * @param result
     * @return com.tarzan.common.core.base.BaseResponse 
     * @author liujinshan
     * @data 2021/11/12 15:09
     */
    @PostMapping(value = "insert",produces = {"application/json"},consumes = {"application/json"})
    public BaseResponse add(@RequestBody @Validated RouteDefinition routeDefinition, BindingResult result){
        try {
            gateWayDynamicRouteService.add(routeDefinition);
        } catch (BusinessException e) {
            logger.info("GateWayDynamicRouteController -> add() BusinessException ex="+e);
            return BaseResponse.error(e.getCode(),e.getMsg());
        } catch (Exception e) {
            logger.error("GateWayDynamicRouteController -> add() BusinessException ex="+e);
            return BaseResponse.error("500",e.getMessage());
        }
        return BaseResponse.ok();
    }


    /**
     * @title update
     * @description TODO 修改路由
     * @param routeDefinition
     * @param result
     * @return com.tarzan.common.core.base.BaseResponse 
     * @author liujinshan
     * @data 2021/11/12 15:09
     */
    @PostMapping(value = "update",produces = {"application/json"},consumes = {"application/json"})
    public BaseResponse update(@RequestBody @Validated RouteDefinition routeDefinition, BindingResult result){
        try {
            gateWayDynamicRouteService.update(routeDefinition);
        } catch (BusinessException e) {
            logger.info("GateWayDynamicRouteController -> update() BusinessException ex="+e);
            return BaseResponse.error(e.getCode(),e.getMsg());
        } catch (Exception e) {
            logger.error("GateWayDynamicRouteController -> update() BusinessException ex="+e);
            return BaseResponse.error("500",e.getMessage());
        }
        return BaseResponse.ok();
    }


    /**
     * @title delete
     * @description TODO 删除路由
     * @param id
     * @return com.tarzan.common.core.base.BaseResponse 
     * @author liujinshan
     * @data 2021/11/12 15:09
     */
    @PostMapping(value = "delete",produces = {"application/json"},consumes = {"application/json"})
    public BaseResponse delete(@RequestBody  String id){
        try {
            gateWayDynamicRouteService.delete(id);
        } catch (BusinessException e) {
            logger.info("GateWayDynamicRouteController -> delete() BusinessException ex="+e);
            return BaseResponse.error(e.getCode(),e.getMsg());
        } catch (Exception e) {
            logger.error("GateWayDynamicRouteController -> delete() BusinessException ex="+e);
            return BaseResponse.error("500",e.getMessage());
        }
        return BaseResponse.ok();
    }

    /**
     * @title refresh
     * @description TODO 刷新路由
     * @param 
     * @return com.tarzan.common.core.base.BaseResponse 
     * @author liujinshan
     * @data 2021/11/12 15:18
     */
    public BaseResponse refresh(){
        try {
            gateWayDynamicRouteService.refresh();
        } catch (BusinessException e) {
            logger.info("GateWayDynamicRouteController -> delete() BusinessException ex="+e);
            return BaseResponse.error(e.getCode(),e.getMsg());
        } catch (Exception e) {
            logger.error("GateWayDynamicRouteController -> delete() BusinessException ex="+e);
            return BaseResponse.error("500",e.getMessage());
        }
        return BaseResponse.ok();
    }

}
